<?php

/**
 * medio_texto actions.
 *
 * @package    hipermedio
 * @subpackage medio_texto
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class medio_textoActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    $this->medio_textos = Doctrine_Core::getTable('MedioTexto')
      ->createQuery('a')
      ->execute();
  }

  public function executeShow(sfWebRequest $request)
  {
    $this->medio_texto = Doctrine_Core::getTable('MedioTexto')->findOneByNodoId(array($request->getParameter('nodo_id')));
    $this->forward404Unless($this->medio_texto);
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new MedioTextoForm();
    $nodo_id = $request->getParameter('id');
    $this->nodo = Doctrine::getTable('Nodo')->findOneById($nodo_id);
    
    $ws = $this->form->getWidgetSchema();
    $ws['nodo_id']->setAttribute('value', $nodo_id);
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new MedioTextoForm();

    $medio = $this->processForm($request, $this->form);

    if($medio){
        return $this->renderPartial('show', array('medio' => $medio));
    }
    else{
        $this->setTemplate('new');
    }
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($medio_texto = Doctrine_Core::getTable('MedioTexto')->find(array($request->getParameter('id'))), sprintf('Object medio_texto does not exist (%s).', $request->getParameter('id')));
    $this->form = new MedioTextoForm($medio_texto);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($medio_texto = Doctrine_Core::getTable('MedioTexto')->find(array($request->getParameter('id'))), sprintf('Object medio_texto does not exist (%s).', $request->getParameter('id')));
    $this->form = new MedioTextoForm($medio_texto);

    $medio = $this->processForm($request, $this->form);

    if($medio){
        return $this->renderPartial('show', array('medio' => $medio));
    }
    else{
        $this->setTemplate('edit');
    }
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($medio_texto = Doctrine_Core::getTable('MedioTexto')->find(array($request->getParameter('id'))), sprintf('Object medio_texto does not exist (%s).', $request->getParameter('id')));
    $medio_texto->delete();

    $this->redirect('medio_texto/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $medio_texto = $form->save();
      
      $nodo = $medio_texto->getNodo();
      $nodo->setTipoMedio('texto');
      $nodo->save();
      
      return $medio_texto;
//      $this->redirect('nodo/show?id='.$medio_texto->getNodoId());
    }
  }
}
